/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package View;

import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

import View.Model.Topic;

/**
 *
 * @author ANA
 */
public class TopicPage extends javax.swing.JFrame {
    JTree loArvore;
    /**
     * Creates new form TopicPage
     */
    private Topic topic;
    private int type;
    
    
    public TopicPage(Topic topic, JTree loArvore) {
        this.topic = topic;
        this.loArvore = loArvore;
        initComponents();
    }

    public TopicPage(JTree loArvore){
        initComponents();
        this.loArvore = loArvore;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtTopicName = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        rbPublish = new javax.swing.JRadioButton();
        rbSuscribe = new javax.swing.JRadioButton();
        rbPubSub = new javax.swing.JRadioButton();
        btOk = new javax.swing.JButton();
        btCancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
        jLabel1.setText("Topic Configuration");

        jLabel2.setFont(new java.awt.Font("Times New Roman", 0, 12)); // NOI18N
        jLabel2.setText("Topic name:");

        jLabel3.setFont(new java.awt.Font("Times New Roman", 0, 12)); // NOI18N
        jLabel3.setText("Type:");

        buttonGroup1.add(rbPublish);
        rbPublish.setText("Publish");
        rbPublish.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbPublishActionPerformed(evt);
            }
        });

        buttonGroup1.add(rbSuscribe);
        rbSuscribe.setText("Subscribe");
        rbSuscribe.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbSuscribeActionPerformed(evt);
            }
        });

        buttonGroup1.add(rbPubSub);
        rbPubSub.setText("Publish/Subscribe");
        rbPubSub.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbPubSubActionPerformed(evt);
            }
        });

        btOk.setText("OK");
        btOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btOkActionPerformed(evt);
            }
        });

        btCancel.setText("Cancel");
        btCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(136, 136, 136)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(25, 25, 25)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(rbPublish)
                            .addComponent(txtTopicName, javax.swing.GroupLayout.PREFERRED_SIZE, 224, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(rbSuscribe)
                            .addComponent(rbPubSub)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(72, 72, 72)
                        .addComponent(btOk, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(37, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(36, 36, 36)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtTopicName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(rbPublish))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(rbSuscribe)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(rbPubSub)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btOk)
                    .addComponent(btCancel))
                .addContainerGap(19, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    
    
    
    private void rbPublishActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbPublishActionPerformed
        // TODO add your handling code here:
        type = 1;
        
    }//GEN-LAST:event_rbPublishActionPerformed

    private void btOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btOkActionPerformed
        
        DefaultTreeModel treeModel = (DefaultTreeModel)loArvore.getModel();
        DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) loArvore.getLastSelectedPathComponent();
        DefaultMutableTreeNode newNode = new DefaultMutableTreeNode("<<Topic>> " + this.txtTopicName.getText());
        treeModel.insertNodeInto(newNode, selectedNode, selectedNode.getChildCount());
        
        this.setVisible(false);
        
        topic.setType(type);
        topic.setName(txtTopicName.getName());
        System.out.println(txtTopicName.getName());
        super.dispose();
    }//GEN-LAST:event_btOkActionPerformed

    private void rbSuscribeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbSuscribeActionPerformed
        // TODO add your handling code here:
        type = 2;
        
    }//GEN-LAST:event_rbSuscribeActionPerformed

    private void rbPubSubActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbPubSubActionPerformed
        // TODO add your handling code here:
        type = 3;
    }//GEN-LAST:event_rbPubSubActionPerformed

    private void btCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCancelActionPerformed
        // TODO add your handling code here:
        super.dispose();
    }//GEN-LAST:event_btCancelActionPerformed
                    

    
    /**
     * @param args the command line arguments
     */
    

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btCancel;
    private javax.swing.JButton btOk;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JRadioButton rbPubSub;
    private javax.swing.JRadioButton rbPublish;
    private javax.swing.JRadioButton rbSuscribe;
    private javax.swing.JTextField txtTopicName;
    // End of variables declaration//GEN-END:variables
}
